package com.example.testing.ipcdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class SecondActivity extends AppCompatActivity {
    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.e(TAG, "onCreate: sUserId:" + UserManager.sUserId);
        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, ThridActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        recoverFromFile();
    }

    /** 反序列化 **/
    public void recoverFromFile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ObjectInputStream in = null;
                try {
                    in = new ObjectInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/cache.text"));
                    User user = (User) in.readObject();
                    Log.e(TAG, "recoverFromFile: user:" + user);
                    Log.e(TAG, "recoverFromFile: urid:" + user.userId + ",userName:" + user.userName + ",isMale:" + user.isMale);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
